<template>
  <el-card>
    <Headers :inp-show="true" @onSerach="onSerach" />
    <el-table stripe :data="goods" border style="width: 100%; margin-top:15px">
      <el-table-column label="#" type="index" />
      <el-table-column prop="order_number" label="订单编号" />
      <el-table-column prop="order_price" label="订单价格" width="80" />
      <el-table-column prop="pay_status" label="是否付款" width="200">
        <template v-slot="{row}">
          <el-tag v-if="row.pay_status === '0'" :type="types">未付款</el-tag>
          <el-tag v-else type="success">已付款</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="is_send" label="是否发货" width="80" />
      <el-table-column prop="create_time" label="下单时间" :formatter="dateFormat" />
      <el-table-column label="操作">
        <el-row type="flex" justify="center">
          <el-button type="primary" icon="el-icon-edit" circle @click="nestedIshow = true" />
          <el-button type="success" icon="el-icon-location" circle @click="Inquiry" />
        </el-row>
      </el-table-column>
    </el-table>
    <!-- 修改弹层 -->
    <el-dialog :visible.sync="nestedIshow" title="修改地址">
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="88px">
        <el-form-item label="省市区/县" prop="valueOrigin">
          <el-select
            v-model="ruleForm.valueProvince"
            placeholder="请选择省"
            style="width:120px"
            @change="changeProvince"
          >
            <el-option
              v-for="item in provinceList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <el-select
            v-model="ruleForm.valueCity"
            placeholder="请选择市"
            style="width:120px"
            @change="changeCity"
          >
            <el-option
              v-for="item in cityOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <el-select
            v-model="ruleForm.valueOrigin"
            placeholder="请选择区"
            style="width:120px"
            @change="changeOrigin"
          >
            <el-option
              v-for="item in originOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="ruleForm.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="end" style="margin-bottom: -10px; margin-top: 40px">
            <el-button size="small" @click="cancel">取消</el-button>
            <el-button size="small" type="primary" @click="isOK">确定</el-button>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 物流弹层 -->
    <el-dialog :visible.sync="logisticsIshow" title="物流进度">
      <el-timeline :reverse="reverse">
        <el-timeline-item
          v-for="(activity, index) in activities"
          :key="index"
          :timestamp="activity.timestamp"
        >
          {{ activity.content }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
    <!-- 分页 -->
    <el-pagination
      :current-page="pages.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pages.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pages.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
</template>

<script>
import jsonData from './china_address.js'
import Headers from '@/components/Headers'
import { ordersGetList, kuaidi } from '@/api/orders'
export default {
  name: 'Nested',
  components: { Headers },
  props: {},
  data() {
    return {
      provinceList: [], // 省列表
      cityList: [], // 市列表
      originList: [], // 区列表

      cityOptions: [], // 市下拉框数据
      originOptions: [], // 区下拉框数据
      pages: {
        query: null,
        pagenum: 1,
        pagesize: 5,
        total: 0
      },
      goods: [],
      types: 'danger',
      ruleForm: {
        valueProvince: '', // 所选的省
        valueCity: '', // 所选的市
        valueOrigin: '', // 所选的区
        address: '',
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      rules: {
        valueOrigin: [{ required: true, message: '请选择完毕', trigger: 'blur' }],
        address: [{ required: true, message: '请输入详细地址', trigger: 'blur' }]
      },
      nestedIshow: false,
      logisticsIshow: false,
      reverse: true,
      activities: [{
        content: '活动按期开始',
        timestamp: '2018-04-15'
      }, {
        content: '通过审核',
        timestamp: '2018-04-13'
      }, {
        content: '创建成功',
        timestamp: '2018-04-11'
      }]
    }
  },
  computed: {}, // 计算属性
  watch: {},
  created() {
    this.ordersGetList()
    this._getJsonData()
  },
  mounted() {
  },
  updated() {
  },
  methods: {
    async ordersGetList() {
      try {
        const { data } = await ordersGetList(this.pages)
        console.log(data.data)
        this.goods = data.data.goods
        this.pages.pagenum = Number(data.data.pagenum)
        this.pages.total = data.data.total
        this.$message.success('页面刷新成功!~')
      } catch (error) {
        console.log(error)
      }
    },
    handleSizeChange(val) {
      this.pages.pagesize = val
      this.ordersGetList()
    },
    handleCurrentChange(val) {
      this.pages.pagenum = val
      this.ordersGetList()
    },
    dateFormat(row, column) {
      var date = row[column.property]
      if (date === undefined) {
        return ''
      }
      var moment = require('moment')
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    onSerach(value) {
      this.pages.query = value
      this.ordersGetList()
    },
    isOK() {
      this.$refs.ruleForm.validate().then(
        response => {
          console.log(response)
          this.cancel()
        },
        error => {
          console.log(error.message)
        }
      )
    },
    cancel() {
      this.$refs.ruleForm.resetFields()
      this.nestedIshow = false
    },
    // 查询
    async Inquiry() {
      this.logisticsIshow = true
      // const { data } = await kuaidi(1106975712662)
      // console.log(data)
    },
    // 选择省
    changeProvince(val) {
      console.log(this.provinceList)
      this.provinceList.forEach((province, index) => {
        if (val.toString() === this.provinceList[index].value) {
          this.cityOptions = this.provinceList[index].children
          this.valueCity = ''
          this.originOptions = this.provinceList[index].children[0].children
          this.valueCity = ''
          this.valueOrigin = ''
          // 如果选中省之后想市区默认选中
          // this.valueCity = this.provinceList[index].children[0].value
          // this.valueOrigin = this.provinceList[index].children[0].children[0].value
        }
      })
    },
    // 选择市
    changeCity(val) {
      this.cityList.forEach((city, index) => {
        if (val.toString() === this.cityList[index].value) {
          this.originOptions = this.cityList[index].children
          this.valueOrigin = ''
          // 如果选中市之后默认区选中
          // this.valueOrigin = this.cityList[index].children[0].value
        }
      })
    },
    // 选择区
    changeOrigin(val) {
      this.valueOrigin = val
    },
    // 初始化省市区数据
    _getJsonData() {
      this.provinceList = []
      this.cityList = []
      this.originList = []
      jsonData.forEach((item) => {
        if (item.value.match(/0000$/)) {
          this.provinceList.push({
            value: item.value,
            label: item.name,
            children: []
          })
        } else if (item.value.match(/00$/)) {
          this.cityList.push({
            value: item.value,
            label: item.name,
            children: []
          })
        } else {
          this.originList.push({
            value: item.value,
            label: item.name
          })
        }
      })
      for (const index in this.provinceList) {
        for (const index1 in this.cityList) {
          if (this.provinceList[index].value.slice(0, 2) === this.cityList[index1].value.slice(0, 2)) {
            this.provinceList[index].children.push(this.cityList[index1])
          }
        }
      }
      for (const item1 in this.cityList) {
        for (const item2 in this.originList) {
          if (this.originList[item2].value.slice(0, 4) === this.cityList[item1].value.slice(0, 4)) {
            this.cityList[item1].children.push(this.originList[item2])
          }
        }
      }
    }

  }
}
</script>

<style scoped></style>
